#pragma once
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<assert.h>
using namespace std;
namespace mutou
{
	template <class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;
		const_iterator begin()const
		{
			return _start;
		}
		const_iterator end()const
		{
			return _finish;
		}
		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		size_t size()const 
		{
			return _finish - _start;
		}
		size_t capacity()const
		{
			return _end_of_storage - _start;
		}
		vector()
		{
		}
		vector(int n,const T& val = T())
		{
			reserve(n);
			for (int i = 0; i < n; i++)
			{
				push_back(val);
			}
		}
		vector(size_t n, const T& val = T())
		{
			reserve(n);
			for (size_t i = 0; i < n; i++)
			{
				push_back(val);
			}
		}
		template <class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}
		vector(initializer_list<T> il)
		{
			reserve(il.size());
			for (auto& e : il)
			{
				push_back(e);
			}

		}
		vector(const vector<T>& vect)
		{
			reserve(vect.capacity());
			for (auto& e: vect)
			{
				push_back(e);
			}
		}
		void  swap(vector<T>& vect)
		{
			std::swap(_start, vect._start);
			std::swap(_finish, vect._finish);
			std::swap(_end_of_storage, vect._end_of_storage);
		}
		vector<T> operator=(vector<T> val)
		{
			swap(val);
			return *this;
		}
		~vector()
		{
			delete[] _start;
			_start=_finish =_end_of_storage = nullptr;
		}
		T& operator[](size_t n)
		{
			assert(n <= size());
			assert(n >= 0);
			return *(_start + n);
		}
		void reserve(size_t n)
		{
			size_t old_size = size();
			if (n > capacity())
			{
				T* tmp  = new T[n];
				if (_start)
				{
					for (size_t i = 0; i < old_size; i++)
					{
						tmp[i] = _start[i];
					}
					delete[] _start;
				}
				_start = tmp;
				_finish = _start + old_size;
				_end_of_storage = _start + n;


			}
		}
		void push_back(const T& x)
		{
			if (_finish == _end_of_storage)
			{
				size_t newcpacacity = capacity() == 0 ? 4 : 2 * capacity();
				reserve(newcpacacity);
			}
			*_finish = x;
			_finish++;
		}
		void pop_back()
		{
			assert(_finish > _start);
			_finish--;
		}
		void insert(iterator pos, const T& x)
		{
			if (_finish == _end_of_storage)
			{
				size_t len = pos - _start;
				size_t newcpacacity = capacity() == 0 ? 4 : 2 * capacity();
				reserve(newcpacacity);
				pos = len + _start;
			}
			iterator it = _finish - 1;
			while (it>= pos)
			{
				*(it + 1) = *it;
				it--;
			}
			*pos = x;
			_finish++;
		}
		iterator erase(iterator pos)
		{
			assert(pos >= _start);
			assert(pos < _finish);
			iterator it = pos + 1;
			while (it <_finish)
			{
				*(it-1) = *it;
				it++;
			}
			_finish--;
			return pos;
		}
		void resize(size_t n, T val = T())
		{
			if (n > size())
			{
				reserve(n);
				while (_finish != _start + n)
				{
					*_finish = val;
					_finish++;
				}

			}
			else
			{
				_finish = _start + n;
			}
		}
	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _end_of_storage= nullptr;
	};
}